﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Century.Web.Security.Attributes;
using Telerik.Web.Mvc;
using Century.Common.Business.BOL;
using Century.Common.Business.BOL.Common;
using Century.Models.Common;
using Century.Web.UI.MVC;
using Century.Business.Repository;

namespace Century.Admin.Controllers
{
    [Authorization(Group = "", Name = "FAQ")]
    public class Mt4FAQController : CenturyAdminBaseController
    {
        FAQBSO db = new FAQBSO();
        //
        // GET: /FAQ/

        public ActionResult Index()
        {


            //ViewData["Section"] = new SelectList(db.GetAll(c => c.CountrySiteId == PersistData.CurrentSite.Id).Where(r => r.Section.Length > 0).Select(r=> new {Section=r.Section }).Distinct(), "Section", "Section");
            List<SelectListItem> faqType = new List<SelectListItem>();
            faqType.Add(new SelectListItem { Text = "SCA", Value = "0", });
            faqType.Add(new SelectListItem { Text = "CMS", Value = "1", });


            ViewBag.FAQType = faqType;

            return View();
        }

        [GridAction]
        public ActionResult GetCountryMt4FAQList()
        {
            ViewData["Section"] = new SelectList(db.GetAll(c => c.CountrySiteId == PersistData.CurrentSite.Id).Where(r => r.Section.Length > 0).Select(r => new { Section = r.Section }).Distinct(), "Section", "Section");

            if (db.CountOfRecord(c => c.CountrySiteId == PersistData.CurrentSite.Id) == 0)
            {
                CopyForCountrySite(PersistData.CurrentSite.Id);
            }

            var model = new GridModel
            {
                Data = db.GetAll(c => c.CountrySiteId == PersistData.CurrentSite.Id).OrderBy(c => c.OrderNo)
            };

            
            List<SelectListItem> faqType = new List<SelectListItem>();
            faqType.Add(new SelectListItem { Text = "SCA", Value = "0", });
            faqType.Add(new SelectListItem { Text = "CMS", Value = "1", });



            ViewBag.FAQType = faqType;



            return View(model);
        }

        private void CopyForCountrySite(int id)
        {
            var values = db.GetAll(c => c.CountrySiteId == 14);
            foreach (var item in values)
            {
                FAQ mt4FAQ = new FAQ
                {
                    CountrySiteId = id,
                    Answer = item.Answer,
                    OrderNo = item.OrderNo,
                    Question = item.Question,
                };

                using (FAQBSO insertDb = new FAQBSO())
                {
                    insertDb.Insert(mt4FAQ);
                }
            }
        }

        [GridAction]
        [AcceptVerbs(HttpVerbs.Post), ValidateInput(false)]
        public ActionResult EditCountryFAQ(int Id)
        {
            string ownedSitePathes = PersistData.CurrentSite.Path;
            FAQ CountryFAQ = db.Find(c => c.Id == Id);
            TryUpdateModel(CountryFAQ);
            db.Update(CountryFAQ);
            var model = new GridModel
            {
                Data = db.GetAll(c => c.CountrySiteId == PersistData.CurrentSite.Id).OrderBy(c => c.OrderNo)
            };
            return View(model);
        }
        [HttpPost]
        [GridAction]
        [AcceptVerbs(HttpVerbs.Post), ValidateInput(false)]
        public ActionResult InsertCountryFAQ(FAQ faq)
        {

            faq.Section = faq.Type == 0 ? "SCA" : faq.Section;
            int CountrySiteId = PersistData.CurrentSiteId;
            faq.CountrySiteId = CountrySiteId;


            if (ModelState.IsValid)
            {
                db.Insert(faq);
            }
            var model = new GridModel
            {
                Data = db.GetAll(c => c.CountrySiteId == PersistData.CurrentSite.Id).OrderBy(c => c.OrderNo)
            };
            List<SelectListItem> faqType = new List<SelectListItem>();
            faqType.Add(new SelectListItem { Text = "SCA", Value = "0", });
            faqType.Add(new SelectListItem { Text = "CMS", Value = "1", });
            ViewBag.FAQType = faqType;
            return View(model);
        }

        [GridAction]
        [HttpPost]
        public ActionResult DeleteCountryFAQ(int Id)
        {

            FAQ CountryFAQ = db.Find(c => c.Id == Id);
            db.Delete(CountryFAQ);

            var model = new GridModel
            {
                Data = db.GetAll(c => c.CountrySiteId == PersistData.CurrentSite.Id).OrderBy(c => c.OrderNo)
            };
            return View(model);
        }
    }
}
